package cn.jingyuan.swan.system.domain.sys.dao.mysql;

import cn.jingyuan.swan.cloud.dao.DaoException;
import cn.jingyuan.swan.cloud.dao.mybatisplus.AbstractMyBatisPlusCrudDao;
import cn.jingyuan.swan.system.domain.sys.dao.RoleDao;
import cn.jingyuan.swan.system.domain.sys.entity.Role;
import cn.jingyuan.swan.system.domain.sys.mapper.RoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class RoleDaoMySql extends AbstractMyBatisPlusCrudDao<Role, Long> implements RoleDao {

    @Resource
    RoleMapper mapper;

    @Override
    public Role getByRoleCode(String roleCode) {
        LambdaQueryWrapper<Role> wrapper = this.queryWrapper().eq(Role::getRoleCode, roleCode);

        try {
            return mapper.selectOne(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public boolean isExist(Long roleId) {
        LambdaQueryWrapper<Role> wrapper = this.queryWrapper().eq(Role::getRoleId, roleId);

        try {
            Integer row = mapper.selectCount(wrapper);
            return row > 0;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public boolean isExist(String roleCode) {
        LambdaQueryWrapper<Role> wrapper = this.queryWrapper().eq(Role::getRoleCode, roleCode);

        try {
            Integer row = mapper.selectCount(wrapper);
            return row > 0;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public int countByRoleId(Long roleId) {
        LambdaQueryWrapper<Role> wrapper = this.queryWrapper().eq(Role::getRoleId, roleId);

        try {
            return mapper.selectCount(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public BaseMapper<Role> getMapper() {
        return this.mapper;
    }

}
